package cn.iocoder.ny.module.mall.controller.admin.favorites;


import cn.iocoder.ny.module.mall.annotation.Encrypt;
import cn.iocoder.ny.module.mall.controller.admin.favorites.dto.MallProductFavoriteDTO;
import cn.iocoder.ny.module.mall.controller.admin.favorites.vo.MallProductFavoritePageVo;
import cn.iocoder.ny.module.mall.domain.MallBanner;
import cn.iocoder.ny.module.mall.domain.MallUserFavorites;
import cn.iocoder.ny.module.mall.service.IMallUserFavoritesService;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.idempotent.core.annotation.Idempotent;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.security.PermitAll;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;


/**
 * @author luchunhong
 * @date 2024/08/10/ 10:45
 * @description:商品收藏
 */

@RestController
@RequestMapping("productFavorites")
public class MallProductFavorites {

    @Autowired
    private IMallUserFavoritesService mallUserFavoritesService;
    // 取消收藏
    @PostMapping("cancellationFavorite")
    @Idempotent(timeout = 1, message = "重复请求，请稍后重试")
    public CommonResult cancellationFavorite(@RequestBody @Validated(MallUserFavorites.Delete.class) MallUserFavorites mallUserFavorites) {
        Integer i = mallUserFavoritesService.cancellationFavorite(mallUserFavorites);
        if (i == 0){
            return error(500,"取消收藏失败");
        }
        return success("取消收藏成功");
    }

    // 新增收藏
    @PostMapping("addFavorites")
    @Idempotent(timeout = 1, message = "重复请求，请稍后重试")
    public CommonResult addProductFavorites(@RequestBody MallUserFavorites mallUserFavorites) {
        Integer i = mallUserFavoritesService.addFavorites(mallUserFavorites);
        if (i == 0){
            return error(500,"收藏失败,该商品已在您的收藏列表内");
        }
        return success("收藏成功");
    }

    // 收藏列表查询
//    @Encrypt
    @PostMapping("queryFavoritesList")
    public CommonResult<PageResult<MallProductFavoriteDTO>> queryFavoritesList(@RequestBody MallProductFavoritePageVo mallProductFavoriteDTO) {
        PageResult<MallProductFavoriteDTO> mallProductFavoriteDTOS = mallUserFavoritesService.queryFavoritesList(mallProductFavoriteDTO);
        return success(mallProductFavoriteDTOS);
    }
    /**
     * 查询当前商品是否收藏
     */
//    @Encrypt
    @PostMapping("queryFavorites")
    public CommonResult<MallUserFavorites> queryFavorites(@RequestBody MallUserFavorites mallUserFavorites) {
        return success(mallUserFavoritesService.queryFavorites(mallUserFavorites));
    }


}
